package com.hnevc.liangxiangju;

/**
 *
 * 记录子串在整串中出现的次数
 */
public class Demo04 {

    public static void main(String[] args) {
        String str ="abeqweraszxccxxwec";
        String key ="we";
       int count= getKeyStringCount(str,key);
       System.out.println(count);
    }
    //获取子串在整串中出现的次数的方法
    public static int getKeyStringCount(String str,String key){
        int count =0; //记录总次数
        if (!str.contains(key)){
            return count;
        }
        int index =0;
        while ((index = str.indexOf(key))!=-1){
            str=str.substring(index+key.length());
            count++;
        }
        return count;
    }
}
